/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.com.ingsw.dao;


import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import mx.com.ingsw.dao.exceptions.NonexistentEntityException;
import mx.com.ingsw.model.Horariom;

/**
 *
 * @author Schwanstei
 */
public class HorariomDAO implements Serializable {
	private EntityManager em;

  	public EntityManager getEntityManager() {
  	EntityManagerFactory emf = Persistence.createEntityManagerFactory("ABCPU");
  	EntityManager em=emf.createEntityManager();
  	return em;
  	}
    
    public void create(Horariom horariom) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(horariom);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
    public Horariom read(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Horariom.class, id);
        } finally {
            em.close();
        }
    }

    public void update(Horariom horariom) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            horariom = em.merge(horariom);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = horariom.getIdHorariom();
                if (read(id) == null) {
                    throw new NonexistentEntityException("The horariom with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void delete(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Horariom horariom;
            try {
                horariom = em.getReference(Horariom.class, id);
                horariom.getIdHorariom();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The horariom with id " + id + " no longer exists.", enfe);
            }
            em.remove(horariom);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Horariom> findHorariomEntities() {
        return findHorariomEntities(true, -1, -1);
    }

    public List<Horariom> findHorariomEntities(int maxResults, int firstResult) {
        return findHorariomEntities(false, maxResults, firstResult);
    }

    private List<Horariom> findHorariomEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Horariom.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

   

    public int getHorariomCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Horariom> rt = cq.from(Horariom.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
